<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/10/10
 * Time: 16:12
 */

namespace app\models;


use app\librarys\Tools;

Class ApplyInstitution
{
    /**
     * 保存创建机构记录
     * @param $data
     * @return bool
     */
    public static function save($data)
    {
        $id = Tools::db_uuid();

        $sql = 'INSERT INTO t_apply_for_institution_record(id,institution_code,create_manager_id,create_datetime,institution_name,contacter,contacter_phone,apply_for_info)
                VALUES (:id,:institution_code,:create_manager_id,now(),:institution_name,:contacter,:contacter_phone,:apply_for_info)';

        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $cmd->bindValue(':institution_code', $data['institution_code']);
        $cmd->bindValue(':create_manager_id', $data['create_manager_id']);
        $cmd->bindValue(':institution_name', $data['txtName']);
        $cmd->bindValue(':contacter', $data['txtContacter']);
        $cmd->bindValue(':contacter_phone', $data['txtContacterPhone']);
        $cmd->bindValue(':apply_for_info', json_encode($data['apply_for_info']));

        $result = $cmd->execute();
        if ($result > 0) {
            return $id;
        }
        return false;
    }

    /**
     * 获取创建机构记录
     * @param $record_id
     * @return array|false|null
     */
    public static function get($record_id)
    {
        $sql = 'SELECT * FROM t_apply_for_institution_record WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $record_id);
        $result = $cmd->queryOne();
        if ($result == false) {
            return null;
        }
        return $result;
    }

    /**
     * 根据机构code获取机构信息
     * @param $institutionCode
     * @return array|false|null
     */
    public static function getInstitutionInfo($institutionCode)
    {
        $sql = 'SELECT * FROM t_apply_for_institution_record WHERE institution_code = :institution_code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':institution_code',$institutionCode);
        $result = $cmd->queryOne();
        if ($result == false) {
            return null;
        }
        return $result['apply_for_info'];
    }
}